const {run}=require('runjs')
const chalk=require('chalk')
const config=require('../vue.config.js')
// process.argv 属性返回一个数组，这个数组包含了启动Node.js进程时的命令行参数
// 输入命令：node scripts/build.js "web-runtime-cjs,web-server-renderer"
// 结果：
// console.log(process.argv[0])   // 打印 D:\nodeJs\node.exe
// console.log(process.argv[1])   // 打印 E:\Study_document\vue-resource\vue-dev\scripts\build.js
// console.log(process.argv[2])   // 打印 web-runtime-cjs,web-server-renderer
const rawArg=process.argv.slice(2)
const args=rawArg.join(' ')

if(rawArg.includes('--preview')){
    //run('vue-cli-service build')
    const port=8081
    const publicPath=config.publicPath

    var app=require('connect')() //中间件的模块化组件，可以用express或koa代替
    var serveStatic = require('serve-static')
    app.use(
        publicPath,
        serveStatic('./dist', {
          index: ['index.html', '/']
        })
      )
      
     app.listen(port,function(){
         console.log(chalk.green(`preview at http://localhost:${port}${publicPath}`))
     }) 
}